package com.jiezhuo.controller.jw;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.LoginUser;
import com.jiezhuo.entity.cmeduSchool.JwXqStudent;
import com.jiezhuo.entity.jw.*;
import com.jiezhuo.service.IJwXqNjXjService;
import com.jiezhuo.service.IJwXqStudentService;
import com.jiezhuo.service.jw.*;
import com.jiezhuo.service.jzg.JzgNewMenuService;
import com.jiezhuo.utils.IDUtils;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.parser.Entity;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.jiezhuo
 * @since 2024-06-19
 */
@RestController
@RequestMapping("/t-stu")
@Api(tags = "学生管理")
public class TStuController {
    @Autowired
    TStuService tStuService;

    @Autowired
    JzgNewMenuService jzgNewMenuService;
    @Autowired
    DmXzqService dmXzqService;

    @Autowired
    JwUserlistService jwUserlistService;

    @ApiOperation("是否存在学生")
    @GetMapping("getStudentExist")
    public R getStudentExist(TStu  s){
        boolean idNumber=true;
                if( s.getSfzjlx().equals("1")){
                    idNumber= IDUtils.isIDNumber(s.getSfzjh());
                }
           if(!idNumber){
               return  R.err().msg("请输入正确的身份证件号");
           }
        QueryWrapper<TStu> tStuQueryWrapper = new QueryWrapper<>();
           tStuQueryWrapper.eq("sfzjh",s.getSfzjh());
        TStu tStu = tStuService.getBaseMapper().selectOne(tStuQueryWrapper);
        if(ObjectUtils.isEmpty(tStu)){
            return R.ok().data("pd",false);
        }
        return  R.ok().data("pd",true).data("stu",tStu);
    }
    @ApiOperation("获得省份")
    @GetMapping("getXzm")
    public  R getXzm(){
        QueryWrapper<DmXzq> dmXzqQueryWrapper = new QueryWrapper<>();
//        dmXzqQueryWrapper.apply(" CHARINDEX('0000','DM') and  ZT=1 ORDER BY ID ");
        List<DmXzq> dmXzqs = dmXzqService.getXzm();
        return  R.ok().data("list",dmXzqs);
    }

    @Autowired
    IJwXqNjXjService jwXqNjXjService;

    @Autowired
    IJwXqStudentService jwXqStudentService;

    @ApiOperation("添加学生")
    @PostMapping("saveStu")
    @Transactional
    public R saveStu(@RequestBody  TStu  s){
        boolean idNumber=true;
        if( s.getSfzjlx().equals("1")){
            idNumber= IDUtils.isIDNumber(s.getSfzjh());
        }
        if(!idNumber){
            return  R.err().msg("请输入正确的身份证件号");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);




        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
               return R.err().msg("没有此权限");
            }
        }
        QueryWrapper<TStu> tStuQueryWrapper1 = new QueryWrapper<>();
        tStuQueryWrapper1.eq("sfzjh",s.getSfzjh());
        int insert =0;
        com.jiezhuo.entity.cmeduSchool.JwXqStudent jwXqStudent = new com.jiezhuo.entity.cmeduSchool.JwXqStudent();
        TStu tStu1 = tStuService.getBaseMapper().selectOne(tStuQueryWrapper1);

        jwXqStudent.setBj(s.getStuid());
        jwXqStudent.setStuId(s.getStuid());
        jwXqStudent.setNewXqdm(s.getXqdm());
        jwXqStudent.setNj(s.getNj());
        jwXqStudent.setBj(s.getBj());
        jwXqStudent.setXh(s.getXh());
        jwXqStudent.setCkxz(s.getCkxz());
        jwXqStudent.setBsks(s.getBsks());
        jwXqStudent.setXslb(s.getXslb());
        jwXqStudent.setGlfs(s.getGlfs());
        jwXqStudent.setDqzt(s.getDqzt());

        if(ObjectUtils.isEmpty(tStu1)){
            QueryWrapper<TStu> tStuQueryWrapper = new QueryWrapper<>();
            tStuQueryWrapper.select("max(STUID) as STUID");
            TStu tStu = tStuService.getBaseMapper().selectOne(tStuQueryWrapper);
            QueryWrapper<com.jiezhuo.entity.cmeduSchool.JwXqNjXj> jwXqNjXjQueryWrapper = new QueryWrapper<>();
            jwXqNjXjQueryWrapper.select(" max(XQ) as XQ");
            com.jiezhuo.entity.cmeduSchool.JwXqNjXj jwXqNjXj = jwXqNjXjService.getBaseMapper().selectOne(jwXqNjXjQueryWrapper);
            s.setStuid(String.format("%08d", Integer.parseInt(tStu.getStuid()) +1));
            s.setXq(jwXqNjXj.getXq());
            s.setOperateTime(new Date());
            jwXqStudent.setXq(jwXqNjXj.getXq());
            QueryWrapper<com.jiezhuo.entity.cmeduSchool.JwXqStudent> xqStu = new QueryWrapper<>();
            xqStu.select("max(ID) as ID");
            com.jiezhuo.entity.cmeduSchool.JwXqStudent jwXqStudent1 = jwXqStudentService.getBaseMapper().selectOne(xqStu);
            jwXqStudent.setId(jwXqStudent1.getId()+1);
            jwXqStudent.setOperator(userId);
            jwXqStudent.setOperateTime(new Date());
            insert = tStuService.getBaseMapper().insert(s);
            jwXqStudentService.getBaseMapper().insert(jwXqStudent);
        }else {
            s.setStuid(tStu1.getStuid());
            insert = tStuService.getBaseMapper().updateById(s);
            jwXqStudent.setStuId(tStu1.getStuid());
            jwXqStudentService.getBaseMapper().updateById(jwXqStudent);

        }

        return  insert>0?R.ok().msg("添加成功"):R.err().msg("添加失败");
    }
    @ApiOperation("查询学生")
    @GetMapping("selectStu")
    public R selectStu( StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }else {
                s.setXqdm("'" + s.getXqdm() + "'");
            }
        }
        R r= tStuService.selectStu(s);
        return r;
    }
    @ApiOperation("查询学校行政班级学生")
    @GetMapping("selectSchoolClassStudent")
    public R selectSchoolClassStudent(StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }
        }
        return  tStuService.selectSchoolClassStudent(s);
    }
    @ApiOperation("查询学校行政班级")
    @GetMapping("selectSchoolClass")
    public R selectSchoolClass(StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }
        }
        return  tStuService.selectSchoolClass(s);
    }

    @ApiOperation("查询学校行政班级列表")
    @GetMapping("selectSchoolClassList")
    public R selectSchoolClassList(StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }
        }

        return  tStuService.selectSchoolClassList(s);
    }

    @ApiOperation("查询学校走班班级列表")
    @GetMapping("selectSchoolClassZb")
    public R selectSchoolClassZb(StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }
        }


        return  tStuService.selectSchoolClassZb(s);
    }
    @ApiOperation("查询学校走班年级对应课程")
    @GetMapping("selectSchoolClassZbCourse")
    public R selectSchoolClassZbCourse(StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){

                return R.err().msg("没有此权限");
            }
        }
        return  tStuService.selectSchoolClassZbCourse(s);
    }
    @ApiOperation("查询学校走班年级对应课程详情")
    @GetMapping("selectSchoolClassZbCourseInfo")
    public R selectSchoolClassZbCourseInfo(StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString(

            ));
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }
        }
        return  tStuService.selectSchoolClassZbCourseInfo(s);
    }
    @ApiOperation("查询历史学生")
    @GetMapping("selectHistoryStu")
    public R selectHistoryStu(StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString(
            ));
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }
        }
        return  tStuService.selectHistoryStu(s);
    }

    @ApiOperation("查询历史学生详情")
    @GetMapping("selectHistoryStuInfo")
    public R selectHistoryStuInfo (StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString(

            ));
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                return R.err().msg("没有此权限");
            }
        }
        return  tStuService.selectHistoryStuInfo(s);
    }


    @Autowired
    JwXqKskcStuFbService jwXqKskcStuFbService;
    @ApiOperation("行政分班")
    @PostMapping("updateXzFb")
    public R updateXzFb(@RequestBody JwXqStudent s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();
        R userPermissionInfo = jzgNewMenuService.getUserPermissionInfo(userId);
        int i = jwXqStudentService.getBaseMapper().updateById(s);

        return R.ok();
    }
    @ApiOperation("走读分班")
    @PostMapping("updateZdFb")
    public R updateZdFb(@RequestBody JwXqKskcStuFb s){

        if(!ObjectUtils.isEmpty(s.getId())){
            int i = jwXqKskcStuFbService.getBaseMapper().updateById(s);
        }else {
            jwXqKskcStuFbService.getBaseMapper().insert(s);
        }
        return R.ok();
    }

    @ApiOperation("走读班查询分班数据")
    @GetMapping("selectXkClassXk")
    public R selectXkClassXk( StuVo s){

      ArrayList<HashMap<String,Object>>  arrayList= jwXqStudentService.selectXkClassXk(s);

        return R.ok().data("list",arrayList);
    }

    @ApiOperation("参加考试学生")
    @GetMapping("selectKsStudent")
    public R selectKsStudent( StuVo s){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String userId=loginUser.getUser().getUserid();

        R userPermissionInfo = jwUserlistService.getUserPermissionInfo(userId);
        if(ObjectUtils.isEmpty(s.getXqdm())){
            s.setXqdm(userPermissionInfo.getData().get("schools").toString());
        }else {
            Set<String> collect = Arrays.stream(userPermissionInfo.getData().get("schools").toString().split(",")).collect(Collectors.toSet());
            boolean contains = collect.contains("'" + s.getXqdm() + "'");
            if (!contains){
                s.setXqdm("'not'");
            }else {
                s.setXqdm("'" + s.getXqdm() + "'");
            }
        }
        return  jwXqStudentService.selectKsStudent(s);
    }
    @ApiOperation("参加考试学生详情")
    @GetMapping("selectKsStudentInfo")
    public R selectKsStudentInfo( StuVo s){
        return  jwXqStudentService.selectKsStudentInfo(s);
    }


}

